local DIY = require "packages.diy_utility.diy_utility"

local skel = fk.CreateSkill{
  name = "sj_dianjuren__lihuaqingxie",
}

Fk:loadTranslationTable{
  ["sj_dianjuren__lihuaqingxie"] = "礼花倾泻",
  [":sj_dianjuren__lihuaqingxie"] = "出牌阶段限一次，你可以重铸一张牌，然后你可以移去手牌区和中央区合计三张类别各不同或花色均相同的牌，若如此做，你分配1点火焰伤害。",
  ["#sj_dianjuren__lihuaqingxie"] = "礼花倾泻：你可以重铸一张牌",
  ["#sj_dianjuren__lihuaqingxie_ask"] = "礼花倾泻：你可以移去手牌区和中央区合计三张类别各不同或花色均相同的牌，然后分配1点火焰伤害",
  ["#sj_dianjuren__lihuaqingxie_damage"] = "礼花倾泻：请分配1点火焰伤害",
  ["$RemoveCenterArea"] = "%from 因 “%arg” 移去了中央区的 %card",
}

skel:addEffect("active", {
  prompt = "#sj_dianjuren__lihuaqingxie",
  target_filter = Util.FalseFunc,
  card_filter = function (self, player, to_select, selected, selected_targets)
    return #selected < 1
  end,
  card_num = 1,
  min_card_num = 1,
  max_card_num = 1,
  can_use = function (self, player)
    return #player:getCardIds("he") > 0 and player:usedSkillTimes(skel.name,Player.HistoryPhase) == 0
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    room:recastCard(effect.cards,player,skel.name)
    local s, ret = room:askToUseActiveSkill(player,{
      skill_name = "#sj_dianjuren__lihuaqingxie_choose",
      prompt = "#sj_dianjuren__lihuaqingxie_ask",
      extra_data = {
        skillName = skel.name,
        extra_data = {
        cards = DIY.getCenterCards(room),
      }},
    })
    if s and ret and ret.cards then
      local hand,center = {},{}
      for _, id in ipairs(ret.cards) do
        if table.contains(player:getCardIds("h"),id) then
          table.insert(hand,id)
        else
          table.insert(center,id)
        end
      end
      if #hand > 0 then
        room:sendLog{
          type = "$PutToDiscard",
          arg = #hand,
          card = hand,
        }
        room:moveCardTo(hand,Card.DiscardPile,nil,fk.ReasonJustMove,skel.name,nil,true,player)
      end
      if #center > 0 then
        room:sendLog{
          type = "$RemoveCenterArea",
          from = player.id,
          arg = skel.name,
          card = center,
        }
        DIY.removeCenterCards(player,center)
      end
      local to = room:askToChoosePlayers(player,{
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        targets = room:getAlivePlayers(),
        cancelable = false,
        prompt = "#sj_dianjuren__lihuaqingxie_damage",
      })[1]
      room:damage{
        from = player,
        to = to,
        damage = 1,
        damageType = fk.FireDamage,
        skillName = skel.name,
      }
    end
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return skel